/*
 *  Copyright 2013 National Institute of Advanced Industrial Science and Technology
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.sss.common;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.sss.mapreduce.GroupID;
import org.sss.mapreduce.StorageNode;
import org.sss.mapreduce.SssException;

@SuppressWarnings("serial")
public class CommonStatus extends RemoteStatus implements Serializable {
  private static final Accumulater<GroupID, Long> plusAccumulater1 = new PlusAccumulater<GroupID>();
  private static final Accumulater<String, Long> plusAccumulater2 = new PlusAccumulater<String>();
  private static final Accumulater<GroupID, Long> inputAccumulater = new InputAccumulater();
  private static final Accumulater<GroupID, Map<StorageNode, Long>> outCountAccumulater = 
    new MapAccumulater<GroupID, StorageNode, Long>(new PlusAccumulater<StorageNode>());
  public Map<GroupID, Long> completedSubTaskCount = new HashMap<GroupID, Long>();
  public Map<GroupID, Long> inputTupleCount = new HashMap<GroupID, Long>();
  public Map<GroupID, Long> inputTupleNBytes = new HashMap<GroupID, Long>();
  public Map<GroupID, Map<StorageNode, Long>> outputTupleCount  = new HashMap<GroupID, Map<StorageNode, Long>>();
  public Map<GroupID, Map<StorageNode, Long>> outputTupleNBytes = new HashMap<GroupID, Map<StorageNode, Long>>();
  public Map<String, Long> counter = new HashMap<String, Long>();;

  public void accumulate(CommonStatus other) throws SssException {
    accumulate(other, true);
  }

  public synchronized void accumulate(CommonStatus other, boolean updateCounter) throws SssException {
    accumulateMap(completedSubTaskCount, other.completedSubTaskCount, plusAccumulater1);
    accumulateMap(inputTupleCount, other.inputTupleCount, inputAccumulater);
    accumulateMap(inputTupleNBytes, other.inputTupleNBytes, inputAccumulater);
    accumulateMap(outputTupleCount,  other.outputTupleCount, outCountAccumulater);
    accumulateMap(outputTupleNBytes, other.outputTupleNBytes, outCountAccumulater);

    if (updateCounter) {
      counter = other.counter;
    }
    else {
      accumulateMap(counter, other.counter, plusAccumulater2);
    }
  }

  private interface Accumulater<K, V> {
    V accumulate(K key, V lhs, V rhs) throws SssException;
  }

  private static class PlusAccumulater<K> implements Accumulater<K, Long> {
    public Long accumulate(K key, Long lhs, Long rhs) {
      return lhs + rhs;
    }
  }

  private static class MapAccumulater<T, K, V> implements Accumulater<T, Map<K, V>> {
    private Accumulater<K, V> accumulater;

    public MapAccumulater(Accumulater<K, V> accumulater) {
      this.accumulater = accumulater;
    }

    public Map<K, V> accumulate(T key, Map<K, V> lhs, Map<K, V> rhs) throws SssException {
      return accumulateMap(lhs, rhs, accumulater);
    }
  }

  private static <K, V> Map<K, V> accumulateMap(Map<K, V> target, Map<K, V> src, Accumulater<K, V> accumulater) throws SssException {
    for (Map.Entry<K, V> e: src.entrySet()) {
      V m = target.get(e.getKey());
      target.put(e.getKey(), 
          (m == null)?e.getValue():accumulater.accumulate(e.getKey(), m, e.getValue()));
    }
    return target;
  }

  private static class InputAccumulater implements Accumulater<GroupID, Long> {
    public Long accumulate(GroupID key, Long lhs, Long rhs) throws SssException {
      if (key.isBroadcast()) {
        if (!lhs.equals(rhs)) {
          throw new SssException("Number of input from broadcasted data is mismatch.");
        }
        return lhs;
      }
      else {
        return lhs + rhs;
      }
    }
  }
}

